package com.shouyun.common.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class DataUtils {
    public static final String USER = "user.json";
    public static final String TOKEN = "token.json";
    public static final String PARAMS = "params.json";
    public static final String SHARE = "share.json";

    /**
     * 初始化创建文件夹
     */
    public static String initDir() {
        String dirPath = "data";
        if (!FileUtils.isExists(dirPath)) {
            FileUtils.mkdirs(dirPath);
        }
        return dirPath + "/";
    }

    /**
     * 读取文本数据
     *
     * @param key
     * @return
     */
    public static JSONObject readData(String key) {
        String dirPath = initDir();
        String filePath = dirPath + key;
        File file = new File(filePath);
        if (!file.exists()) {
            FileUtils.writeText(filePath, "", false);
        }
        String text = FileUtils.readText(filePath);
        if (StringUtils.isBlank(text)) {
            return new JSONObject();
        }
        return JSONObject.parseObject(text);
    }

    /**
     * 写入文本数据
     *
     * @param key
     * @return
     */
    public static void writeText(String key, String text) {
        String dirPath = initDir();
        FileUtils.writeText(dirPath + key, text, false);
    }

    /**
     * 设置信息
     *
     * @param tableKey
     * @param key
     * @param data
     */
    public static void setData(String tableKey, String key, Object data) {
        JSONObject resultData = readData(tableKey);
        resultData.put(key, data);
        writeText(tableKey, JSONObject.toJSONString(resultData));
    }

    /**
     * 设置信息
     *
     * @param tableKey
     * @param key
     */
    public static void removeData(String tableKey, String key) {
        JSONObject resultData = readData(tableKey);
        resultData.remove(key);
        writeText(tableKey, JSONObject.toJSONString(resultData));
    }

    /**
     * 获取信息
     *
     * @param tableKey 表
     * @param key      标识
     * @return
     */
    public static JSONObject getData(String tableKey, String key) {
        JSONObject resultData = readData(tableKey);
        JSONObject result=resultData.getJSONObject(key);
        if(result==null){
            result=new JSONObject();
        }
        return result;
    }


    /**
     * 获取信息-全部
     *
     * @param tableKey 表
     * @return
     */
    public static JSONObject getData(String tableKey) {
        return readData(tableKey);
    }

    /**
     * 设置参数
     *
     * @param key
     * @param data
     */
    public static void setParams(String key, Object data) {
        JSONObject resultData = readData(DataUtils.PARAMS);
        resultData.put(key, data);
        writeText(DataUtils.PARAMS, JSONObject.toJSONString(resultData));
    }

    /**
     * 获取参数
     *
     * @param key
     * @return
     */
    public static String getParams(String key) {
        JSONObject resultData = readData(DataUtils.PARAMS);
        return resultData.getString(key);
    }
}
